package com.aofeng.hybridwork.android.task;

import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.android.NativeBaseMethod;
import com.aofeng.hybrid.android.task.SimpleAsyncTask;
import com.aofeng.hybrid.service.EntityService;
import com.aofeng.hybrid.service.SqlService;
import com.aofeng.hybrid.util.CommUtil;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by LGY on 2016-09-17.
 */
public class SavePaperTask  extends SimpleAsyncTask{
    public SavePaperTask(HybridActivity hybridActivity, JSONObject json) {
        super(hybridActivity, json);
    }

    @Override
    protected JSONObject action() throws Exception {
        JSONObject data  = json.getJSONObject("data");
        String f_check_item_id = data.getString("f_check_item_id");
        String f_checker_id = data.getString("f_checker_id");
        JSONObject row = data.getJSONObject("row");
        return savePaper(row, f_check_item_id, f_checker_id, hybridActivity, false);
    }

    public static JSONObject savePaper(JSONObject row, String f_check_item_id, String f_checker_id, HybridActivity hybridActivity, boolean repairSlip) throws Exception {
        EntityService es = new EntityService(hybridActivity);
        SqlService ss = new SqlService(hybridActivity);

        ss.run("delete from t_check_paper_meter where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_plumb where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_valve where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_innerplumb where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_cooker where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_alarm where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_heater where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_furnace where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper_defect where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        ss.run("delete from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "'");

        //更新经纬度
        row.put("f_longitude", CommUtil.getSharedPreference(hybridActivity.getApplicationContext(), "longitude"));
        row.put("f_latitude", CommUtil.getSharedPreference(hybridActivity.getApplicationContext(), "latitude"));

        //如果审核状态为打回，则变为重检
        if(row.has("f_approved") && !row.isNull("f_approved") && row.getString("f_approved").equals("打回")) {
            row.put("f_approved", "重检");
            row.put("f_approved_note", "");
        }

        if(row.has("id")) {
            es.append("t_check_paper", row);
        } else {
            es.save("t_check_paper", row);
        }

        if(repairSlip)
            ss.run("update t_check_paper set f_repaired ='" + row.getString("f_repaired") + "', f_repaired_uploaded='未传' where f_check_item_id = '" + f_check_item_id + "'");
        else
            ss.run("update t_check_plan_item set f_state='已检', f_upload_state='未传' where id = '" + f_check_item_id + "'");
        row.put("code", 200);
        return row;
    }

}
